<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            function testWeightBagProblem(weight, value, size) {
                let dp = new Array(value.length).fill().map(() => new Array(size + 1).fill(0))
                for (let i = 0; i < value.length; i++) {
                    //当背包容量为0时，最大价值自然为0
                    dp[i][0] = 0
                }
                for (let j = 0; j <= size; j++) {
                    if (j >= weight[0]) {
                        dp[0][j] = value[0]
                    }
                }
                for (let i = 1; i < value.length; i++) {
                    for (let j = 0; j <= size; j++) {
                        if (j < weight[i]) {
                            dp[i][j] = dp[i - 1][j]
                        } else {
                            dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i])
                        }
                    }
                }
                console.log(dp)
            }
            testWeightBagProblem([1, 3, 4], [15, 20, 30], 4)
        </script>
    </body>
</html>
